How to do it?:

Submission: Submit the link on Github of the assignment to Canvas


  1. Install two packages gganimate and gifski then restart Rstudio. Using the Adult Census Income data, make an animation using geom_point and transition_states.
library(ggplot2)
library(gganimate)
library(gifski)
library(tidyverse)
library(knitr)
library(lubridate)

df <- read.csv('https://bryantstats.github.io/math421/data/adult_census.csv')
df <- as.tibble(df)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` instead.
## ℹ The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
df %>% 
  filter(native.country == "Canada") %>% 
  ggplot(aes(x= hours.per.week, y = age, color= income))+
    geom_point()+
    transition_states(sex)+
    labs(title = 'Sex: {closest_state}')

  1. Using the Adult Census Income data, make an animation using geom_bar and transition_states.
df %>% 
  filter(native.country == "Canada") %>% 
  ggplot(aes(x= income,fill= sex))+
    geom_bar(position = 'fill')+
    transition_states(race)+
    labs(title = 'Income: {closest_state}')

  1. Use the WHO’s dataset at this link. Make a top-10 bar race by months between countries on the number of deaths by Covid 19 in 2021.
library(tweenr)
df1 <- read.csv('WHO-COVID-19-global-data.csv')
df1 <- as_tibble(df1)
df1$Date_reported <- as.Date(df1$Date_reported)
df1$year <- year(df1$Date_reported)
df1$month <- month(df1$Date_reported)

d1 <- df1 %>% 
  filter(year == 2021)%>% 
  group_by(year, Country) %>% 
  summarise(sum = sum(Cumulative_deaths)) 

d2 <- d1 %>% 
  group_by(year) %>% 
  mutate(rank=rank(-sum))

d1 <- df1 %>% 
  group_by(year, Country) %>% 
  summarise(mean = mean(Cumulative_deaths))
d2 <- d1 %>% group_by(year) %>% mutate(rank=rank(-mean)) 
d3 <- d2 %>% filter(rank <= 10)
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=Country, fill=Country, label=Country)) + geom_col()+
    geom_text(aes(y = mean, label = Country), hjust = 1.4)+ 
    coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
    labs(title = 'Year {closest_state}', x='', y='Total Number of Positive Caeses', fill='Country')+
    theme(plot.title = element_text(hjust = 1, size = 22),
          axis.ticks.y = element_blank(),
          axis.text.y  = element_blank()) + 
    transition_states(year)+
    ease_aes("cubic-in-out")
animate(a1, nframes = 400)

  1. Make a bar race using a dataset of your own interest. You may use the dataset that we use in class (https://covidtracking.com/data/download/all-states-history.csv) but you should make a different bar racev from ones in the slides.
df2 <- read.csv("all-states-history.csv")
df2 <- as_tibble(df2)
df2$date <- as.Date(df2$date)
df2$month <- month(df2$date)

d1 <- df2 %>% group_by(month, state) %>% summarise(sum = sum(death))
d2 <- d1 %>% group_by(month) %>% mutate(rank=rank(-sum)) 
d3 <- d2 %>% filter(rank <= 10)

a1 <- d3 %>% ggplot(aes(x=rank, y=sum, group=state, fill=state, label=state)) +  geom_col()+
    geom_text(aes(y = sum, label = state), hjust = 1.4)+ 
    coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
    labs(title = 'Month {closest_state}', x='', y='Total Number of Positive Caeses', fill= 'state')+
    theme(plot.title = element_text(hjust = 1, size = 22),
          axis.ticks.y = element_blank(),
          axis.text.y  = element_blank()) + 
    transition_states(month)+
    ease_aes("cubic-in-out")
animate(a1, nframes = 400)